/*
 * Created by JFormDesigner on Tue Jun 25 11:12:44 CST 2024
 */

package scct.zhang.ui;

import scct.zhang.dao.UserDao;
import scct.zhang.model.User;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.List;

/**
 * @author 张
 */
public class FormLogin extends JFrame {
    public FormLogin() {
        initComponents();
        initView();
    }

    private void initView(){
        UserDao userDao=new UserDao();
        List<User> allUser=userDao.getAllUser();
        for (User user:allUser){
            comUser.addItem(user.getUsername());

        }

    }

    private void btnlogin(ActionEvent e) {
//        System.out.println("按钮点击了");
//        获取文本框的输入用户名和密码
        String name = comUser.getSelectedItem().toString();
        String pass = txtpass.getText();
        System.out.println(name + "," + pass);
//        name.matches("正则表达式")返回true成功，false失败
        if (!pass.matches("[a-zA-Z0-9_\\u4e00-\\u9fa5]{1,10}$")){
            JOptionPane.showMessageDialog(null,"你输入的密码不合法，请重新检查后输入");
            return;
        }
        UserDao userDao = new UserDao();
        User user = userDao.login(name,pass);
        if (user==null){
            JOptionPane.showMessageDialog(this,"用户名或密码错误，请检查后重新输入");
        }else{
            JOptionPane.showMessageDialog(this,"登录成功");
            FormMain formMain = new FormMain();
            formMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            formMain.setSize(1000,700);
//            居中
            formMain.setLocationRelativeTo(null);
//            formMain.set
            formMain.setVisible(true);
//            隐藏当前窗体
            this.dispose();

//            进入主窗体
        }

    }

    private void btnSign(ActionEvent e) {
        // TODO add your code
        FromSign sign = new FromSign();
        sign.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        sign.setVisible(true);
        this.dispose();
//        FormMain formMain = new FormMain();
//        formMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        formMain.setSize(800,600);
////          居中
//        formMain.setLocationRelativeTo(null);
////            formMain.set
//        formMain.setVisible(true);
////            隐藏当前窗体
//        this.dispose();
//            进入主窗体

    }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - zhang
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        btnlogin = new JButton();
        btnSign = new JButton();
        txtpass = new JPasswordField();
        comUser = new JComboBox();

        //======== this ========
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("LoGin In ");
        label1.setFont(label1.getFont().deriveFont(Font.BOLD|Font.ITALIC, label1.getFont().getSize() + 10f));
        contentPane.add(label1);
        label1.setBounds(145, 20, 120, 45);

        //---- label2 ----
        label2.setText("\u7528\u6237\u540d\uff1a");
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(65, 80), label2.getPreferredSize()));

        //---- label3 ----
        label3.setText("\u5bc6\u7801\uff1a");
        contentPane.add(label3);
        label3.setBounds(new Rectangle(new Point(65, 140), label3.getPreferredSize()));

        //---- btnlogin ----
        btnlogin.setText("\u767b\u5f55");
        btnlogin.addActionListener(e -> btnlogin(e));
        contentPane.add(btnlogin);
        btnlogin.setBounds(new Rectangle(new Point(90, 195), btnlogin.getPreferredSize()));

        //---- btnSign ----
        btnSign.setText("\u6ce8\u518c");
        btnSign.addActionListener(e -> btnSign(e));
        contentPane.add(btnSign);
        btnSign.setBounds(new Rectangle(new Point(230, 195), btnSign.getPreferredSize()));
        contentPane.add(txtpass);
        txtpass.setBounds(150, 130, 145, txtpass.getPreferredSize().height);
        contentPane.add(comUser);
        comUser.setBounds(150, 75, 140, comUser.getPreferredSize().height);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 300);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - zhang
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JButton btnlogin;
    private JButton btnSign;
    private JPasswordField txtpass;
    private JComboBox comUser;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
